/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : home.js                                 
*  Description  : 클럽개별메인 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-12  이윤노    Created.  
*  2012-06-14  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){
	
	//동호회가입
	function clsClubJoin(){
		this._HTMLDom=null;
		this.clubJoinData = null;
		
		this._init();	
	}
	
	clsClubJoin.prototype = {
		_init: function(){
			var $layer = $("div#container article.self_home_board");	
			this._HTMLDom = {
				"$layer": $layer	
			}			
		}	
		, setData: function(thisData){
			this.clubJoinData = thisData;
			this.display();	
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];	
			
			$layer.find("div.club_box_info p em").html("["+ this.clubJoinData["동호회명"] +"] 동호회에 가입하시겠습니까?");
		}
		, $getBtnConfirm: function(){
			return this._HTMLDom["$layer"].find("a img[alt=확인]");
		}
		, $getBtnCancel: function(){
			return this._HTMLDom["$layer"].find("a img[alt=취소]");
		}
	}
	

	
	function clsComm()
	{
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
//------------------------ 동호회가입------------------------//				
		, clubJoinSubmit: function(arg){
			var thisClass =  this;
			var szClubSeq = arg["동호회키"];
			
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CL001",
				"servicename":"registMember",			
			}, {
				  "clubSeq": szClubSeq
				  , "joinment": "-"
			}, function(ret){
				// callback and mapping
				var resData = {
					"응답코드": (ret["msp"]["contents"]["serviceResult"] == "00000"?true:false)
					, "메세지":  _gf.getResultMsg(ret,[""])
				}				
				$(thisClass).trigger("evtFinishClubJoinSubmit",[resData]);
			});
			// ajax 끝
			
		}
//------------------------ 동호회가입------------------------//		
	}
	
	function clsControl()
	{
		this.club_seq = null;		
		this.objComm = null;		
		this.objClubJoin = null;
		this.objClubInfo = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			var member_id = _util.getCookie("member_id");
			this.club_seq = _util.getParam("club_seq");			
			
			if(!member_id) {
				if(confirm("로그인 후 이용하여 주세요.")){
					document.objGlobal.goLogin();						
				}else{
					document.location.href = "../club/club.html";
				}
			}else if(!this.club_seq) {
				document.location.href = "../club/club.html";
			}
			
			this._preload();
			
			this.objComm = new clsComm();
			this.objClubJoin = new clsClubJoin();
						
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				
			}
			
			$.ajax({
				url: "js/self_club_top.js"
				, dataType: "script"
				, async: false
				, success: function(){
				
				}
				, error: function(){
					alert("error");
				}
			});
			
			_util.executeThread(function(){
				for(var k in thisClass._permitStart){
					if(!thisClass._permitStart[k]) return false;
				}
				return (_clubControl.getClubInfo().infoData);
			}, function(){			
				_gf.spinEnd();				
				thisClass._start();
			});	
		}
		, _role: function(){
			if(this.objRole.isMember()){
				alert("이미 가입된 회원입니다.");
				document.location.href = "home.html?club_seq=" + this.club_seq;
			}
		}
		, _start: function(){		
			this.objClubInfo = _clubControl.getClubInfo();
			this.objRole = new clsRole(this.objClubInfo.infoData);
			this._role();
			
			//동호회가입			
			this.objClubJoin.setData(this.objClubInfo.infoData);
			this.club_sesq = this.objClubInfo.infoData["동호회키"];
		}
		, _bind: function(){
			var thisClass = this;
			
			// 동호회가입 완료이벤트
			$(this.objComm).bind("evtFinishClubJoinSubmit", function(event, resData){
				alert(resData["메세지"]);
				if(resData["응답코드"]){
					document.location.href = "home.html" + thisClass.returnParam();
				}
			});
			
			//확인버튼 이벤트
			this.objClubJoin.$getBtnConfirm().click(function(event){
				event.preventDefault();				
				var submitData = {
					"동호회키": thisClass.club_seq	
				}
				
				thisClass.objComm.clubJoinSubmit(submitData);
			});
			
			//취소버튼 이벤트
			this.objClubJoin.$getBtnCancel().click(function(event){
				event.preventDefault();				
				document.location.href = "home.html" + thisClass.returnParam();
			});
			
		}
		, returnParam: function(){			
			return "?club_seq=" + this.club_seq;	
		}
	}
	
	document.objControl = new clsControl();
});